package wei.xinya.springAop;

import wei.xinya.springAop.dao.UserDao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.function.IntPredicate;

public class UserDaoProxy implements InvocationHandler {

    private final UserDao userDao;
    private final IntPredicate begin;

    public UserDaoProxy(UserDao userDao, IntPredicate begin) {
        this.userDao = userDao;
        this.begin = begin;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("方法执行之前，方法名：" + method.getName() + "参数：" + Arrays.toString(args));

        boolean allMatch = Arrays.stream(args).allMatch(arg -> begin.test((int) arg));
        if (!allMatch) {
            return -1;
        }

        Object invoke = method.invoke(userDao, args);

        System.out.println("方法执行之后");
        return invoke;
    }
}
